$(function () {
  ketishouzhi = 1;
  initketishouzhiData();
})
//传值(型号名称)
let Name = localStorage.getItem('name');
if (Name) {
  $(".modelName").text(Name);
} else {
  $(".modelName").text('--');
}


function initketishouzhiData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60201,
    chartNo: 1,
    key01: Name
  };
  ApiGet("getdata", obj, function (e) {
    // console.log(e)
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data
    getallminpinxiangmushouzhiTable(data);
  });
}

function getallminpinxiangmushouzhiTable(Tabledata) {
  console.log(Tabledata, "data");
  // let data = [{
  //     code: '150529000000000015079001',
  //     MONEY_1: "2,389,497.00",
  //     MONEY_2: "2,653,345.04",
  //     MONEY_3: "367,815.00",
  //     MONEY_4: "9,733.62",
  //     MONEY_5: "352,400.00",
  //     MONEY_6: "352,400.00",
  //     MONEY_7: "352,400.00",
  //     MONEY_8: "352,400.00",
  //     NAME: '董一平',
  //     PROJECT_NAME: "储热式钛吸热管加工",
  //     total: "6,829,990.66",
  //   },
  //   {
  //     code: '150529000000000015079001',
  //     MONEY_1: "2,389,497.00",
  //     MONEY_2: "2,653,345.04",
  //     MONEY_3: "367,815.00",
  //     MONEY_4: "9,733.62",
  //     MONEY_5: "352,400.00",
  //     MONEY_6: "352,400.00",
  //     MONEY_7: "352,400.00",
  //     MONEY_8: "352,400.00",
  //     NAME: '董一平',
  //     PROJECT_NAME: "喷涂钨管",
  //     total: "6,829,990.66"
  //   },

  //   {
  //     code: '150529000000000015079001',
  //     MONEY_1: "2,389,497.00",
  //     MONEY_2: "2,653,345.04",
  //     MONEY_3: "367,815.00",
  //     MONEY_4: "9,733.62",
  //     MONEY_5: "352,400.00",
  //     MONEY_6: "352,400.00",
  //     MONEY_7: "352,400.00",
  //     MONEY_8: "352,400.00",
  //     NAME: '董一平',
  //     PROJECT_NAME: "喷涂钨管",
  //     total: "6,829,990.66"
  //   },

  //   {
  //     code: '150529000000000015079001',
  //     MONEY_1: "2,389,497.00",
  //     MONEY_2: "2,653,345.04",
  //     MONEY_3: "367,815.00",
  //     MONEY_4: "9,733.62",
  //     MONEY_5: "352,400.00",
  //     MONEY_6: "352,400.00",
  //     MONEY_7: "352,400.00",
  //     MONEY_8: "352,400.00",
  //     PROJECT_NAME: "测试费",
  //     NAME: '董一平',
  //     total: "6,829,990.66"
  //   },
  //   {
  //     code: '150529000000000015079001',
  //     MONEY_1: "2,389,497.00",
  //     MONEY_2: "2,653,345.04",
  //     MONEY_3: "367,815.00",
  //     MONEY_4: "9,733.62",
  //     MONEY_5: "352,400.00",
  //     MONEY_6: "352,400.00",
  //     MONEY_7: "352,400.00",
  //     MONEY_8: "352,400.00",
  //     PROJECT_NAME: "发电机弹性支撑",
  //     NAME: '董一平',
  //     total: "6,829,990.66"
  //   },

  // ]
  let data = Tabledata
  let cols = [
    // 1
    [{
        field: 'key01',
        title: '项目编号',
        rowspan: 3,
        width: 200,
        fixed: "left",
        templet: function (d) {
          return `
          <a onclick="clickTo(this)" projectCode=${d.key02} ; projectName=${d.key01} name=${d.key12}>
            <div>
              <span>${d.key02}</span> 
            </div>
          </a>`
        }
      }, {
        field: 'key02',
        title: '项目名称',
        rowspan: 3,
        width: 200,
        fixed: "left",
        templet: function (d) {
          if (d.key02) {
            return `
          <a onclick="clickTo(this)" projectCode=${d.key02}  projectName=${d.key01} name=${d.key12}>
            <div>
              <span>${d.key01}</span> 
              <span class="name"> | ${d.key12}</span>
            </div>
          </a>`
          } else {
            return ''
          }

        }
      },
      {
        field: 'PROJECT_NAME',
        title: '项目收款',
        colspan: 4,
        sort: true,
        align: 'center',
        // totalRow: true
      },
      {
        field: 'PROJECT_NAME',
        title: '成本支出',
        colspan: 6,
        sort: true,
        align: 'center',
      }
    ],
    // 2
    [{
        field: 'key03',
        title: '期末结余',
        sort: true,
        minWidth: 160,
        align: 'right',
        rowspan: 2,
        templet: function (d) {
          return `<span>${pricefmt((d.key03 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key04',
        title: '本期来款',
        sort: true,
        minWidth: 160,
        rowspan: 2,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key04 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key05',
        title: '本期结算',
        sort: true,
        minWidth: 160,
        rowspan: 2,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key05 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key06',
        title: '本期末结算',
        sort: true,
        minWidth: 160,
        rowspan: 2,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key06 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: '',
        title: '报销成本',
        sort: true,
        colspan: 3,
        align: 'center',
      },
      {
        field: '',
        title: '项目占用资金',
        sort: true,
        colspan: 3,
        align: 'center'
      },
    ],
    // 3
    [{
        field: 'key07',
        title: '期末结余',
        sort: true,
        minWidth: 160,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key07 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key08',
        title: '本期来款',
        sort: true,
        minWidth: 160,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key08 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key09',
        title: '本期结算',
        sort: true,
        minWidth: 160,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key09 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key10',
        title: '本期末结余',
        minWidth: 160,
        sort: true,
        align: 'right',
        templet: function (d) {
          return `<span>${pricefmt((d.key10 / 10000).toFixed(2))}</span>`
        }
      },
      {
        field: 'key11',
        title: '预付款',
        width: 200,
        sort: true,
        // fixed: "right",
        align: "right",
        templet: function (d) {
          return `<span>${pricefmt((d.key11 / 10000).toFixed(2))}</span>`
        }
      }
    ]
  ];
  tableDrawing(data) // 数据传到 table组件
  // pagesDrawing(res) // 数据传到 分页组件

  function tableDrawing(data) {
    layui.use('table', function () {
      let table = layui.table;
      table.render({
        elem: '#newsList',
        cols: cols,
        data: data, // 数据
        // totalRow: true, //合计行区域
        // limit: limit, // 每页显示的条数
        // height: tableHeight,
      });
    })
  }

}

function clickTo(obj) {
  console.log(obj, 'pppppp')

  let projectCode = obj.getAttribute('projectCode');
  let projectName = obj.getAttribute('projectName');
  let personname = obj.getAttribute('name');

  localStorage.setItem('personname', personname); //人名
  localStorage.setItem('projectName', projectName); //项目名称
  localStorage.setItem('projectCode', projectCode); //项目编号
  localStorage.setItem('Name', Name); // 项目名称 （上个页面的 - 民品、军二品...）


  window.location.href = 'threeColumn.html';
}